package fairygui.editor.dialogs
{
   import fairygui.UIPackage;
   import fairygui.editor.ComDocument;
   import fairygui.editor.Consts;
   import fairygui.editor.EditorWindow;
   import fairygui.editor.WindowBase;
   import fairygui.editor.gui.EGComponent;
   import fairygui.editor.gui.EGGraph;
   import fairygui.editor.gui.EGImage;
   import fairygui.editor.gui.EGLoader;
   import fairygui.editor.gui.EGMovieClip;
   import fairygui.editor.gui.EGObject;
   import fairygui.editor.gui.EGRichTextField;
   import fairygui.editor.gui.EGSwfObject;
   import fairygui.editor.gui.EGTextField;
   import fairygui.editor.gui.EPackageItem;
   import fairygui.editor.gui.EUIObjectFactory;
   import fairygui.editor.gui.EUIPackage;
   import fairygui.editor.utils.UtilsStr;
   
   public class ExchangeDialog extends WindowBase
   {
       
      
      private var _source:EGObject;
      
      public function ExchangeDialog(param1:EditorWindow)
      {
         var win:EditorWindow = param1;
         super();
         _editorWindow = win;
         this.contentPane = UIPackage.createObject("Builder","ExchangeDialog").asCom;
         this.centerOn(_editorWindow.groot,true);
         var func:Function = function(param1:String):void
         {
            contentPane.getChild("target").text = param1;
         };
         this.contentPane.getChild("text").addClickListener(function():void
         {
            func("text");
         });
         this.contentPane.getChild("richtext").addClickListener(function():void
         {
            func("richtext");
         });
         this.contentPane.getChild("graph").addClickListener(function():void
         {
            func("graph");
         });
         this.contentPane.getChild("loader").addClickListener(function():void
         {
            func("loader");
         });
         this.contentPane.getChild("ok").addClickListener(__actionHandler);
         this.contentPane.getChild("cancel").addClickListener(closeEventHandler);
      }
      
      public function open(param1:EGObject) : void
      {
         this._source = param1;
         show();
      }
      
      override protected function onHide() : void
      {
         _editorWindow.mainPanel.editPanel.self.requestFocus();
      }
      
      override public function actionHandler() : void
      {
         var newChild:EGObject = null;
         var doc:ComDocument = null;
         var index:int = 0;
         var xml:XML = null;
         var pi:EPackageItem = null;
         var target:String = contentPane.getChild("target").text;
         var comp:EGComponent = this._source.parent;
         var pkg:EUIPackage = comp.pkg;
         try
         {
            if(!UtilsStr.startsWith(target,"ui://"))
            {
               newChild = EUIObjectFactory.createObject2(pkg,target,this._source.editMode);
               if(newChild == null)
               {
                  throw new Error("invalid object type");
               }
            }
            else
            {
               pi = _editorWindow.project.getItemByURL(target);
               if(pi.owner != pkg && !pi.exported)
               {
                  _editorWindow.alert(Consts.g.text12);
                  return;
               }
               if(pi.owner == pkg && pi.id == comp.packageItem.id)
               {
                  _editorWindow.alert(Consts.g.text76);
                  return;
               }
               newChild = EUIObjectFactory.createObject(pi,this._source.editMode);
               if(newChild is EGComponent && (newChild as EGComponent).containsComponent(comp.packageItem))
               {
                  newChild.dispose();
                  _editorWindow.alert(Consts.g.text76);
                  return;
               }
            }
            doc = _editorWindow.mainPanel.editPanel.findComDocument(pkg,comp.packageItem.id);
            index = comp.getChildIndex(this._source);
            xml = this._source.toXML();
            doc.removeSelection(this._source);
            comp.removeChild(this._source);
            if(!(newChild is EGComponent || newChild is EGImage || newChild is EGMovieClip || newChild is EGSwfObject))
            {
               xml.@size = int(this._source.width) + "," + int(this._source.height);
            }
            if(newChild is EGGraph)
            {
               xml.@type = "rect";
            }
            else if(newChild is EGTextField || newChild is EGRichTextField)
            {
               xml.@autoSize = "none";
            }
            else if(newChild is EGLoader)
            {
               if(this._source is EGImage)
               {
                  xml.@url = this._source.resourceURL;
               }
            }
            newChild.fromXML_beforeAdd(xml);
            comp.addChildAt(newChild,index);
            newChild.relations.fromXML(xml);
            newChild.fromXML_afterAdd(xml);
            comp.notifyChildReplaced(this._source,newChild);
            doc.actionHistory.action_childReplace(this._source,newChild);
            doc.setUpdateChildrenPanelFlag();
            doc.addSelection(newChild);
            _editorWindow.mainPanel.editPanel.self.requestFocus();
         }
         catch(err:Error)
         {
            _editorWindow.alertError(err);
            return;
         }
         this.hide();
      }
   }
}
